package com.example.administrator.mypayactivity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/4/14 0014.
 */

public class MainActivity extends Activity {
    private CustomDialog customDialog;
    private View dialogViewPwd ;
    private PasswordInputView vTradePwd;
    private Button button ;
    private String str = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.btn);
        dialogViewPwd = LayoutInflater.from(this).inflate(R.layout.dialog_input_tradepwd, null);
        customDialog = new CustomDialog(this, dialogViewPwd);
        vTradePwd = (PasswordInputView) dialogViewPwd.findViewById(R.id.vTradePwd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
         * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置,
         * 可以直接调用getWindow(),表示获得这个Activity的Window
         * 对象,这样这可以以同样的方式改变这个Activity的属性.
         */
                Window dialogWindow = customDialog.getWindow();
//                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                dialogWindow.setGravity(Gravity.BOTTOM);
//设置dialog没有边距
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
                dialogWindow.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                customDialog.setCanceledOnTouchOutside(true);
                customDialog.show();
            }
        });


        vTradePwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("=========","onTextChanged"+s.toString());
                if (s.length() >= 6) {
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        ininEven();



    }
    private void ininEven () {
//        b1.setOnClickListener(this);
//        b2.setOnClickListener(this);
//        b3.setOnClickListener(this);
//        b4.setOnClickListener(this);

//        设置edittext始终不弹出键盘
        vTradePwd.setInputType(InputType.TYPE_NULL);
    }

    public void clickHandle(View v) {
        switch (v.getId()){
            case R.id.btn_0 :
                str += "0";
                setTextContent(str);
                break;
            case R.id.btn_1 :
                str += "1";
                setTextContent(str);
                break;
            case R.id.btn_2 :
                str += "2";
                setTextContent(str);
                break;
            case R.id.btn_3 :
                str += "3";
                setTextContent(str);
                break;
            case R.id.btn_4 :
                str += "4";
                setTextContent(str);
                break;
            case R.id.btn_5 :
                str += "5";
                setTextContent(str);
                break;
            case R.id.btn_6 :
                str += "6";
                setTextContent(str);
                break;
            case R.id.btn_7 :
                str += "7";
                setTextContent(str);
                break;
            case R.id.btn_8 :
                str += "8";
                setTextContent(str);
                break;
            case R.id.btn_9 :
                str += "9";
                setTextContent(str);
                break;
            case R.id.btn_d :
                if (str.length() <= 1) {
                    str = "";
                } else {
                    str = str.substring(0, str.length() - 1);
                }
                setTextContent(str);
                break;
        }
    }

    private void setTextContent(String content) {
        vTradePwd.setText(content);
    }
}
